package org.ngmj.server.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.ngmj.message.ChatRequestMessage;
import org.ngmj.message.ChatResponseMessage;
import org.ngmj.server.session.SessionFactory;

@ChannelHandler.Sharable
public class ChatRequestHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage message) throws Exception {
        String messageTo = message.getTo();
        Channel channel = SessionFactory.getSession().getChannel(messageTo);
        if (channel != null) {
            channel.writeAndFlush(new ChatResponseMessage(message.getFrom(), message.getContent()));
        } else {
            ctx.writeAndFlush(new ChatResponseMessage(false, "用户不存在,或用户不在线"));
        }
    }
}
